/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lucava.boatskeeper.client.control;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import org.lucava.boatskeeper.client.event.AuthEvent;
import org.lucava.boatskeeper.client.event.AuthHandler;
import org.lucava.boatskeeper.client.event.ConnectEvent;
import org.lucava.boatskeeper.client.event.DisconnectEvent;
import org.lucava.boatskeeper.client.event.boat.ListBoatsEvent;
import org.lucava.boatskeeper.client.view.AuthView;
import org.lucava.boatskeeper.shared.dto.UserDto;
import org.lucava.lib.client.control.Control;
import org.lucava.lib.client.event.InitEvent;
import org.lucava.lib.client.service.SessionService;
import org.lucava.lib.client.service.SessionServiceAsync;
import org.lucava.lib.client.session.SessionManager;

/**
 *
 * @author glenn
 */
public class AuthController extends Control implements AuthHandler {

    private SessionServiceAsync sessionService = GWT.create(SessionService.class);
    private AuthView view;

    @Override
    public void onInit() {
    }

    @Override
    public void onInitHandlers() {
        getEventBus().addHandler(ConnectEvent.TYPE, AuthController.this);
        getEventBus().addHandler(DisconnectEvent.TYPE, AuthController.this);
        getEventBus().addHandler(AuthEvent.TYPE, AuthController.this);
    }

    /**
     * Quand une authentification et requise
     *
     * @param event
     */
    @Override
    public void onUserAuth(AuthEvent event) {
        checkSession();
    }

    @Override
    public void onUserConnect(ConnectEvent event) {

        if (view.isValid()) {
            sessionService.authenticate(view.getUser(), new AsyncCallback<Boolean>() {
                @Override
                public void onSuccess(Boolean result) {
                    getLayoutManager().enableLayout("default");
                  
                    getEventBus().fireEvent(new ListBoatsEvent());
                }

                @Override
                public void onFailure(Throwable caught) {
                   Window.alert("Impossible d'effectuer la connexion\n ->" + caught.getLocalizedMessage());
                }
            });
        }


    }

    @Override
    public void onUserDisconnect(DisconnectEvent event) {
        sessionService.closeSession(new AsyncCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
                getLayoutManager().enableLayout("auth");
                view = new AuthView(getEventBus());
                assignView("content", view);
            }

            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Erreur lors de la déconnexion\n ->" + caught.getLocalizedMessage());
            }
        });
    }

    private void checkSession() {

        sessionService.getUser(new AsyncCallback<UserDto>() {
            @Override
            public void onSuccess(UserDto result) {

                if (result == null) {
                    getLayoutManager().enableLayout("auth");
                    view = new AuthView(getEventBus());
                    assignView("content", view);

                } else {
                    getLayoutManager().enableLayout("default");
                    SessionManager.getInstance().setUser(result);
                    getEventBus().fireEvent(new ListBoatsEvent());
                }
            }

            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Impossible de récupérer l'utilisateur\n ->" + caught.getLocalizedMessage());
            }
        });
    }
}
